/*
 * Copyright 2013-2019 Xia Jun(3979434@qq.com).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 ***************************************************************************************
 *                                                                                     *
 *                        Website : http://www.farsunset.com                           *
 *                                                                                     *
 ***************************************************************************************
 */
package com.workcell.workdata.im.component.cim.pusher;

import com.workcell.workdata.im.component.event.CommentEvent;
import com.workcell.workdata.im.component.event.MomentEvent;
import com.workcell.workdata.im.component.listener.EventMessageRemover;
import com.workcell.workdata.im.constant.ChangeType;
import com.workcell.workdata.im.constant.MessageAction;
import com.workcell.workdata.im.entity.Comment;
import com.workcell.workdata.im.entity.EventMessage;
import com.workcell.workdata.im.entity.Message;
import com.workcell.workdata.im.entity.Moment;
import com.workcell.workdata.im.model.app.Origin;
import com.workcell.workdata.im.repository.CommentRepository;
import com.workcell.workdata.im.repository.MomentRepository;
import com.workcell.workdata.im.service.NotifiableListService;
import org.springframework.context.event.EventListener;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * 推送圈子消息 过滤屏蔽我的人以及被我屏蔽的人
 */
@Component
public class MomentMessagePusher extends BaseMessagePusher {

    @Resource
    private CommentRepository commentRepository;

    @Resource
    private MomentRepository momentRepository;

    @Resource
    private NotifiableListService notifiableListService;

    @Override
    public void push(Message message, Origin origin) {

        long uid = Long.parseLong(message.getContent());

        super.push(message, notifiableListService.findList(uid));
    }

    @EventListener
    @Order(0)
    public void onMomentEvent(MomentEvent event){

        Moment moment = event.getSource();
        Message message = new EventMessage();
        message.setSender(moment.getId());
        message.setContent(String.valueOf(moment.getUid()));

        if (event.getType() == ChangeType.ADD){
            message.setAction(MessageAction.ACTION_500);
        }

        if (event.getType() == ChangeType.DELETE){
            message.setAction(MessageAction.ACTION_501);
        }

        event.setUidList(notifiableListService.findList(moment.getUid()));

        super.push(message,event.getUidList());

        /**
         *删除未接收的评论消息
         * @see EventMessageRemover.onMomentEvent()
         */
        event.setMessage(message);

    }

    @EventListener
    @Order(0)
    public void onCommentEvent(CommentEvent event){
        if (event.getType() == ChangeType.ADD){
            notifyAddComment(event);
        }

        if (event.getType() == ChangeType.DELETE){
            notifyDeleteComment(event);
        }
    }

    /**
     * 有点赞时 | 有新的评论时 发送提醒消息
     */
    private void notifyAddComment(CommentEvent event) {

        Comment comment = event.getSource();

        long momentUid = momentRepository.findUid(comment.getMomentId());
        List<Long> idList = notifiableListService.findList(momentUid,comment.getUid());

        idList.add(momentUid);

        idList.remove(comment.getUid());

        Message message = new EventMessage();
        message.setSender(comment.getMomentId());
        message.setContent(String.valueOf(comment.getId()));
        message.setAction(MessageAction.ACTION_502);
        message.setExtra(String.valueOf(momentUid));

        /*
         * 如果回复别人的评论，传回被评论人ID，用于提醒
         */
        if (Objects.equals(comment.getType(),Comment.ACTION_COMMENT) && comment.getParentId() != null){
            Comment parent = commentRepository.findById(comment.getParentId()).orElse(null);
            message.setTitle(parent == null ? null : String.valueOf(parent.getUid()));
        }

        super.push(message,idList);

        /*
         *删除未接收的评论消息
         *@see EventMessageRemover.onCommentEvent
         */
        event.setMessage(message);
        event.setUidList(idList);
    }


    /**
     * 评论被删除或者被取消点赞，只通知文章作者
     */
    private void notifyDeleteComment(CommentEvent event) {

        Comment comment = event.getSource();

        /*
        评论ID
         */
        long id = comment.getId();
        /*
        评论作者的UID
         */
        long uid = comment.getUid();

        /*
        朋友圈内容ID
         */
        long momentId = comment.getMomentId();
        /*
        朋友圈作者UID
         */
        long momentUid = momentRepository.findUid(momentId);

        List<Long> idList = notifiableListService.findList(momentUid,uid);

        idList.add(momentUid);

        idList.remove(uid);

        Message message = new EventMessage();
        message.setSender(momentId);
        message.setContent(String.valueOf(id));
        message.setAction(MessageAction.ACTION_503);
        message.setExtra(String.valueOf(momentUid));

        super.push(message,idList);


        /*
         *删除未接收的评论消息
         *@see EventMessageRemover.onCommentEvent
         */
        event.setMessage(message);
        event.setUidList(idList);

    }

}
